<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-6-3
 * Func   银行卡类型
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('web_bank');
$a     = $_GET['a'];
if(IS_POST){
    $id = intval($_REQUEST['id']);
    //  表单数据
    $post = $_POST;
     
    $ins = [];
    $ins['name']    =   NoBadStr($post['name']);
    $ins['status']  =   NoBadStr($post['status']);
    $ins['status']  =   $ins['status'] ? 1: 0;
    if($id){
        //修改
        $flag = $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$id);
    }else{
        $flag = $db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
    }
    if ($flag){
        ShowMsg('操作成功',U('user/banktype'));
    }else{
        ShowMsg('操作失败',-1);
    }
}

switch ($a){
    case 'add':
        $page  = 'bank/add';
        break;
    case 'del':
        $id = NoBadStr($_REQUEST['id']);
        if(!$id){
            AjaxReturn(['error'=>'1','msg'=>'参数丢失']);
        }
        $db->exec('DELETE FROM '.$table.' WHERE id = '.$id);
        AjaxReturn(['error'=>'0','msg'=>'删除成功']);
        break;
    case 'sta':
        $id   = NoBadStr($_REQUEST['id']);
        $sta  = NoBadStr($_REQUEST['flag']);
        $upd  = array();
        if ($sta){
            $upd['status'] = 1;
        }else{
            $upd['status'] = 0;
        }
        $flag = $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($upd).' WHERE id = '.$id);
        if ($flag){
            AjaxReturn(['error'=>'0','msg'=>'操作成功']);
        }else{
            AjaxReturn(['error'=>'1','msg'=>'操作失败']);
        }
        break;
    default:
        $data  = array();
        $where = ' FROM '.$table.' t';
        $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
        if($tmp['p']){
            $page = max(1,intval($_GET['page']));
            $size = 20;
            $limit    = ' LIMIT '.($page - 1) * $size.','.$size;
        
            $orderby = ' ORDER BY t.id ASC ';
            $url = '?m='.$m.'&c='.$c.'&a='.$a;
            $data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
            $multi = Multi($url,$page,$tmp['p'],$size);
        }
        $page  = 'bank/list';
        break;
}

$tpl->display($page);
